﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace VAdvantage.Controls
{
    public partial class SmoothExpander : UserControl
    {
        public SmoothExpander()
        {
            InitializeComponent();
        }

        private bool _isExpanded = true;

        private double _width = 180;

        private bool _isRightPane = false;

        public bool IsRightPane
        {
            get
            {
                return _isRightPane;
            }
            set
            {
                _isRightPane = value;
                SetLayout(_isRightPane);
            }
        }

        public bool IsExpanded
        {

            get
            {
                return _isExpanded;
            }
            set
            {
                _isExpanded = value;
                SetExpanded();
            }

        }

        private void SetExpanded()
        {
            if (_isExpanded)
            {
                LayoutRoot.Width = _width;
            }
            else
                LayoutRoot.Width = 22;
            SetLayout(_isRightPane);

        }

        private void SetLayout(bool isRightPane)
        {

            if (isRightPane)
            {
                LayoutRoot.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Star);
                LayoutRoot.ColumnDefinitions[1].Width = GridLength.Auto;
                Grid.SetColumn(grid, 1);
                ((CompositeTransform)path.RenderTransform).ScaleX = -1;
                if (_isExpanded)
                {
                    ((CompositeTransform)path.RenderTransform).Rotation = 0;
                    border.Opacity = 1;
                }
                else
                {
                    ((CompositeTransform)path.RenderTransform).Rotation = 180;
                    border.Opacity = 0;
                }
            }
            else
            {
                LayoutRoot.ColumnDefinitions[1].Width = new GridLength(1, GridUnitType.Star);
                LayoutRoot.ColumnDefinitions[0].Width = GridLength.Auto;
                Grid.SetColumn(grid, 0);
                ((CompositeTransform)path.RenderTransform).ScaleX = 1;
                if (_isExpanded)
                {
                    ((CompositeTransform)path.RenderTransform).Rotation = 0;
                    border.Opacity = 1;
                }
                else
                {
                    ((CompositeTransform)path.RenderTransform).Rotation = 180;
                    border.Opacity = 0;
                }
            }
        }

        public new double Width
        {

            get
            {
                return _width;
            }
            set
            {

                if (value > 0)
                {
                    _width = value;
                }
                SetExpanded();
            }
        }

        public new UIElement Content
        {

            get
            {

                return this.border.Child;
            }

            set
            {
                this.border.Child = value;
            }
        }

        public UIElement Child
        {

            get
            {
                return border.Child;

            }
            set
            {
                border.Child = value;
            }
        }

        public Brush ExpanderBackground
        {
            get
            {
                return LayoutRoot.Background;
            }
            set
            {
                LayoutRoot.Background = value;
            }
        }
        private void SetStoryBoardChildren()
        {
            ((DoubleAnimation)(sbExpand.Children[0])).To = _width;
        }

        private void grid_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {

            if (_isExpanded)
            {
                sbCollapse.Begin();
            }
            else
            {

                if (((DoubleAnimation)sbExpand.Children[0]).To == null)
                {
                    ((DoubleAnimation)sbExpand.Children[0]).To = _width;
                }

                //if (_isRightPane)
                //{

                //}


                sbExpand.Begin();
            }
            _isExpanded = !_isExpanded;
        }

        private void grid_MouseEnter(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "MouseOver", true);
        }

        private void grid_MouseLeave(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "MouseLeave", true);
        }

        private void grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //VisualStateManager.GoToState(this, "MousePress", true);
        }

        //    <i:Interaction.Triggers>
        //    <i:EventTrigger EventName="MouseEnter">
        //        <ei:GoToStateAction StateName="MouseOver" TargetName="grid" TargetObject="{Binding ElementName=userControl}"/>
        //    </i:EventTrigger>
        //    <i:EventTrigger EventName="MouseLeave">
        //        <ei:GoToStateAction StateName="MouseLeave" TargetName="grid" TargetObject="{Binding ElementName=userControl}"/>
        //    </i:EventTrigger>
        //    <i:EventTrigger EventName="MouseLeftButtonDown">
        //        <ei:GoToStateAction StateName="MousePress" TargetName="grid" TargetObject="{Binding ElementName=userControl}"/>
        //    </i:EventTrigger>
        //</i:Interaction.Triggers>

    }
}
